home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5398 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  77 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.clark.net!bms88!stuart
  3. From: stuart@bmsi.com (Stuart D. Gathman)
  4. Subject: Re: SORTing problem c++
  5. Organization: Business Management Systems, Inc., Fairfax, VA
  6. Date: Thu, 1 Feb 1996 22:27:53 GMT
  7. Message-ID: <1996Feb1.222753.20805@bmsi.com>
  8. References: <310B0DD0.34F1@pi.net>
  9.  
  10. In article <310B0DD0.34F1@pi.net> you write:
  11. >I'm used to normal c
  12. >just recent i switched to c++, now i am stucked
  13. >with a program that did work in c but doesn't
  14. >in c++.
  15. >it is a program that sorts the 'argv'command line
  16. >arguments array,
  17. >
  18. >what can I DO???
  19. >p.s. I am using borlands c++
  20. >p.s. thanks for reading this 
  21. >#include <stdio.h>
  22. >#include <stdlib.h>
  23. >#include <string.h>
  24. >
  25. >int sort_function(char **a, char **b)
  26. >{
  27. >   return( strcmp(*a, *b));
  28. >}
  29. >
  30. >
  31. >int main(int argc, char **argv)
  32. >{
  33. >   int
  34. >           x;
  35. >
  36. >   qsort(argv, argc, sizeof(char*), sort_function);
  37. >
  38. >   for (x = 0; x < argc; x++)
  39. >         printf("%s\n", argv[x]);
  40. >   return 0;
  41. >}
  42. >
  43. >******************************************
  44. >these are the error mesages,
  45. >
  46. >
  47. >Error ..\SOURCES\OPDR_45.CPP 17: Cannot convert 'int (*)(char * *,char * 
  48. >*)' to 'int (*)(const void *,const void *)'
  49. >Error ..\SOURCES\OPDR_45.CPP 17: Type mismatch in parameter '__fcmp' in 
  50. >call to 'qsort(void *,unsigned int,unsigned int,int (*)(const void 
  51. >*,const void *))'
  52.  
  53. You program was not portable even in C.  The C++ compiler is trying
  54. to tell you that your sort_function is the wrong type to pass
  55. to qsort().  qsort wants a function with the signature specified in
  56. the first error message:
  57.  
  58. int sort_function(const void *a, const void *b) {
  59.    const char **ap = (const char **)a;
  60.    const char **bp = (const char **)b;
  61.    return( strcmp(*ap, *bp));
  62. }
  63.  
  64. Yes, this is ugly.  This is why C++ supports polymorphism and would
  65. not use qsort except inside a class wrapper.
  66.  
  67. BTW, as written by you, your program would only run on byte addressable
  68. machines where the hardware format of a void * happens to be the
  69. same as a const char **.
  70. -- 
  71. Stuart D. Gathman    <stuart@bmsi.com> / <..!uunet!bms88!stuart>
  72.                 Business Management Systems Inc.
  73.                Phone: 703 591-0911 Fax: 703 591-6154
  74.           "Microsoft is the QWERTY of Operating Systems" - SDG
  75.